package StudentSysterm;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentSystermTest {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //创建一个集合
        ArrayList<Student> list = new ArrayList<>();

        loop:do {
            menu();
            System.out.println("请选择");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    add(list);
                    print(list);
                    break;
                case "2":
                    delete(list);
                    print(list);
                    break;
                case "3":
                    if(list.size()==0){
                        System.out.println("当前无录入信息，请添加后操作");
                        break;
                    }
                    System.out.println("请输入要查询的学号");
                    int id=sc.nextInt();

                    int i=check(list,id);
                    if(i<0){
                        System.out.println("无此学生信息");
                        break;
                    }
                    //打印查询到的信息
                    Student stu=list.get(i);
                    int sid=stu.getId();
                    int age=stu.getAge();
                    String name=stu.getName();
                    String address=stu.getAddress();
                    System.out.println("学号\t姓名\t年龄\t地址");
                    System.out.println(  sid+"\t"+name+"\t"+age+"\t"+address);
                    break;
                case "4":
                    System.out.println("请输入要修改的学号");
                    int id2=sc.nextInt();
                    modify(list,id2);
                    print(list);
                    break;
                case "0":
//                    break loop;//类似于go to
                    System.exit(0);//停止虚拟机运行

                default:
                    System.out.println("输入有误，请重新输入");
                    break;
            }
        } while (true);
    }

    //菜单
    public static void menu() {
        System.out.println("---------------------------------------------");
        System.out.println("-----1.添加学生--------------2.删除学生--------");
        System.out.println("-----3.查找学生--------------4.修改学生信息-----");
        System.out.println("-----0.退出系统--------------------------------");
    }

    //添加
    public static void add(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        //创建对象
        Student stu = new Student();
        int id, age;
        String name, address;
        //输入信息
        while (true) {
            System.out.println("请输入学号");
            id = sc.nextInt();
            int flag = check(list, id);
            if (flag >= 0) {
                System.out.println("学号已重复，请重新输入");
                continue;
            } else {
                break;
            }
        }
            System.out.println("请输入姓名");
            name = sc.next();
            System.out.println("请输入年龄");
            age = sc.nextInt();
            System.out.println("请输入家庭住址");
            address = sc.next();


        //信息添加到对象中
        stu.setId(id);
        stu.setName(name);
        stu.setAge(age);
        stu.setAddress(address);
        //添加到集合中
        list.add(stu);
        System.out.println("添加成功");
    }

    //检查学号唯一性
    public static int check(ArrayList<Student> list, int id) {
        //遍历集合的每个对象的学号
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            int sid = stu.getId();
            //再与形参进行比较
            if (sid == id) {
                return i;
            }
        }
        return -1;
    }
    //删除
    public static void delete(ArrayList<Student> list){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的学号");
        int id=sc.nextInt();
        int flag=check(list,id);
        if(flag>=0){
            //执行删除操作
            list.remove(list.get(flag));
            System.out.println("删除成功");
        }
        else{
            System.out.println("删除失败，没有此学号信息");
        }
    }
    //修改
    public static void modify(ArrayList<Student> list,int id2){
        Scanner sc = new Scanner(System.in);
        //查询到对应的对象
        int i=check(list,id2);
        if(i<0){
            System.out.println("修改失败，没有此学号信息");
            return;
        }
        //输入修改后的信息
        System.out.println("请输入修改后的信息");
        int id, age;
        String name, address;
        while(true){
            System.out.println("请输入学号");
            id = sc.nextInt();
            //检查唯一性
            int d=check(list,id);
            if(d>=0){
                System.out.println("学号重复，请重新输入");
                continue;
            }
            else{
                break;
            }
        }
        System.out.println("请输入姓名");
        name = sc.next();
        System.out.println("请输入年龄");
        age = sc.nextInt();
        System.out.println("请输入家庭住址");
        address = sc.next();
        //修改对象信息
        Student stu=list.get(i);
        stu.setId(id);
        stu.setName(name);
        stu.setAge(age);
        stu.setAddress(address);
    }
    //遍历集合
    public static void print(ArrayList<Student> list){
        System.out.println("学号\t姓名\t年龄\t地址");
        for (int i = 0; i < list.size(); i++) {
            Student stu=list.get(i);
            int id=stu.getId();
            int age=stu.getAge();
            String name=stu.getName();
            String address=stu.getAddress();
            System.out.println(id+"\t"+name+"\t"+age+"\t"+address);
        }
    }
}
